package com.imay.live.activity.family.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.activity.family.FamilyParams;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.FamilyNotifyModel;
import com.imay.live.proto.FamilyResult;
import com.imay.live.proto.family.FamilyAdminLockNotify;
import com.imay.live.proto.family.FamilyApplyNotify;
import com.imay.tbas_api.IMYTBASObject;

/**
 * Created by Administrator on 2017/9/18 0018.
 */

public class FamilyManager {
    private final static String ACTION_FAMILY_CREATE = "action_family_create";
    private final static String ACTION_FAMILY_EDIT = "action_family_edit";
    private final static String ACTION_FAMILY_APPLY = "action_family_apply";
    private final static String ACTION_FAMILY_LVUPGRADE = "action_family_lvupgrade";
    private final static String ACTION_FAMILY_LOCKED = "action_family_locked";

    private IMYTBASObject familyNotify;

    public static class Builder {

        private final FamilyManager.FamilyListener listener;

        public static FamilyManager.Builder create(FamilyManager.FamilyListener listener) {
            if (listener == null) {
                throw new NullPointerException("listener == null");
            }
            return new FamilyManager.Builder(listener);
        }

        protected Builder(FamilyManager.FamilyListener listener) {
            this.listener = listener;
        }

        public FamilyManager build() {
            FamilyManager familyManager = new FamilyManager();
            familyManager.registerFamilyNotify(listener);
            return familyManager;
        }
    }

    public interface FamilyListener {
        void familyCreate();

        void familyLvUpgrade();

        void familyUserApply(FamilyApplyNotify familyApplyNotify);

        void familyEdit();

        void familyBeLocked();

        void familyUnLocked();
    }

    /**
     * 创建家族
     */
    public static void familyCreate(String stringData) {
        FamilyResult<String> result = JSON.parseObject(stringData, new TypeReference<FamilyResult<String>>() {
        }.getType());
        FamilyNotifyModel familyNotifyModel = new FamilyNotifyModel();
        familyNotifyModel.setAction(ACTION_FAMILY_CREATE);
        familyNotifyModel.setData(result.content);
        EventHelper.postFamily(JSON.toJSONString(familyNotifyModel));
    }

    /**
     * 编辑家族
     */
    public static void familyEdit(String stringData) {
        FamilyResult<String> result = JSON.parseObject(stringData, new TypeReference<FamilyResult<String>>() {
        }.getType());
        FamilyNotifyModel familyNotifyModel = new FamilyNotifyModel();
        familyNotifyModel.setAction(ACTION_FAMILY_EDIT);
        familyNotifyModel.setData(result.content);
        EventHelper.postFamily(JSON.toJSONString(familyNotifyModel));
    }

    /**
     * 家族被封
     *
     * @param stringData
     */
    public static void familyLocked(String stringData) {
        FamilyResult<String> result = JSON.parseObject(stringData, new TypeReference<FamilyResult<String>>() {
        }.getType());
        FamilyNotifyModel familyNotifyModel = new FamilyNotifyModel();
        familyNotifyModel.setAction(ACTION_FAMILY_LOCKED);
        familyNotifyModel.setData(result.content);
        EventHelper.postFamily(JSON.toJSONString(familyNotifyModel));
    }

    /**
     * 加入家族(包括审核的信息，以及加入的信息双向)
     * message FamilyApplyNotify {
     * int64    uid = 1;        //申请人UID
     * int64    fromUid = 2;    //邀请人UID
     * int64    familyId =3;    //家族ID
     * int64    createAt = 4;   //申请时间
     * string   explain =5;     //申请描述
     * int32    status = 6;     //状态  1:申请; 2:拒绝; 3:通过; 4:邀请
     * int64       optUid = 7;      //审核管理员uid
     * int64    checkAt = 8;    //审核时间
     * int32    type = 9;       //通知类型 1:入族申请;2:申请结果; 3:邀请入族; 4:邀请结果
     * }
     *
     * @param stringData
     */
    public static void familyUserApply(String stringData) {
        FamilyResult<String> result = JSON.parseObject(stringData, new TypeReference<FamilyResult<String>>() {
        }.getType());
        FamilyNotifyModel familyNotifyModel = new FamilyNotifyModel();
        familyNotifyModel.setAction(ACTION_FAMILY_APPLY);
        familyNotifyModel.setData(result.content);
        EventHelper.postFamily(JSONObject.toJSONString(familyNotifyModel));
    }

    /**
     * 家族和家族用户经验和等级变更通知
     */
    public static void familyLvUpgrade(String stringData) {
        FamilyResult<String> result = JSON.parseObject(stringData, new TypeReference<FamilyResult<String>>() {
        }.getType());
        FamilyNotifyModel familyNotifyModel = new FamilyNotifyModel();
        familyNotifyModel.setAction(ACTION_FAMILY_LVUPGRADE);
        familyNotifyModel.setData(result.content);
        EventHelper.postFamily(JSON.toJSONString(familyNotifyModel));
    }

    public void registerFamilyNotify(final FamilyListener listener) {
        familyNotify = new IMYTBASObject(false);
        familyNotify.stringCommand = EventHelper.EVENT_FAMILY_NOTIFY;
        familyNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject object) {
                FamilyNotifyModel familyNotifyModel = JSON.parseObject(object.stringData, FamilyNotifyModel.class);
                if (ACTION_FAMILY_CREATE.equals(familyNotifyModel.getAction())) {
                    listener.familyCreate();
                } else if (ACTION_FAMILY_EDIT.equals(familyNotifyModel.getAction())) {
                    listener.familyEdit();
                } else if (ACTION_FAMILY_APPLY.equals(familyNotifyModel.getAction())) {
                    FamilyApplyNotify result = JSON.parseObject(familyNotifyModel.getData(), FamilyApplyNotify.class);
                    listener.familyUserApply(result);
                } else if (ACTION_FAMILY_LVUPGRADE.equals(familyNotifyModel.getAction())) {
                    listener.familyLvUpgrade();
                } else if (ACTION_FAMILY_LOCKED.equals(familyNotifyModel.getAction())) {
                    FamilyAdminLockNotify result = JSON.parseObject(familyNotifyModel.getData(), FamilyAdminLockNotify.class);
                    if (result.type == FamilyParams.LOCKED) {
                        listener.familyBeLocked();
                    } else if (result.type == FamilyParams.UNLOCKED) {
                        listener.familyUnLocked();
                    }
                }
            }
        });
        familyNotify.addEventListenWithTBASObject();
    }

    public void unregisterFamilyNotify() {
        if (familyNotify != null) {
            familyNotify.destroy();
        }
    }
}
